﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

var editImages = {
    ready: function() {
        $("li.clickable").click(editImages.imageClick);
        $("#insertEditButton").click(editImages.updateClick);
        $("#deleteButton").click(editImages.deleteClick);
        editImages.pullImageDetails("/content/media/csd.png");
    },

    imageClick: function($e) {
        var url = "";
        if ($e.target.className == "clickable") {
            url = $e.target.id;
        } else {
            url = $e.target.parentElement.id;
        };
        editImages.pullImageDetails(url);
    },
    
    updateClick: function($e) {
        $e.preventDefault;
        editImages.updateImageDetails();
    },

    deleteClick: function($e) {
        $e.preventDefault;
        editImages.deleteImage();
    },

    // Function to update or add images.
    updateImageDetails: function() {
        editImages.disableForm("Updating");
        // use JSON method to send the information to the server
        var iName = $("#imageNameTextBox").val();
        var iUrl = $("#imageUrlTextBox").val();
        var iType = $("#imageTypeTextBox").val();
        var iTag = $("#imageTagTextBox").val();
        $.getJSON("/admin/UpdateImageDetails",
        { imageName: iName, imageUrl: iUrl, imageType: iType, imageTag: iTag }, function(data) {
            // Check the result for ok message
            if (data == "ok") {
                // Result ok, so re-load the list
                editImages.disableForm("Retrieving");
                editImages.redisplayImageList();
            } else {
                $("#resultText").value(data);
            }
            editImages.enableForm();
        });
    },

    // Function to update or add images.
    deleteImage: function() {
        editImages.disableForm("Deleting");
        // use JSON method to send the information to the server
        var iName = $("#imageNameTextBox").val();
        $.getJSON("/admin/DeleteImage",
        { imageName: iName }, function(data) {
            // Check the result for ok message
            if (data == "ok") {
                // Result ok, so re-load the list
                editImages.disableForm("Retrieving");
                editImages.redisplayImageList();
            } else {
                $("#resultText").value(data);
            }
            editImages.enableForm();
        });
    },

    // Pull new list of images
    redisplayImageList: function() {
        $.get("/admin/GetImageList", null, function(html) {
            $("#imageList li").remove();
            var temp = $('<div/>').html(html);
            var items =
                temp.find('li')
                    .appendTo('#imageList')
                    .hide()
                    .fadeIn(1000);
            // reset the click check
            $("li.clickable").click(editImages.imageClick);
        });
    },

    pullImageDetails: function(urlName) {
        editImages.disableForm("Retrieving");
        $.getJSON("/Admin/GetImageDetails",
        { imageUrl: urlName }, function(data) {
            $("#selectedImage").attr("src", data.ImageURL);
            $("#imageNameTextBox").attr("value", data.ImageName);
            $("#imageUrlTextBox").attr("value", data.ImageURL);
            $("#imageTypeTextBox").attr("value", data.ImageType);
            $("#imageTagTextBox").attr("value", data.ImageTag);
            editImages.enableForm();
        });
    },

    disableForm: function(msg) {
        $("#editImageLegend").text("Image Information (" + msg + ")");
        $("#insertEditButton").attr("disabled", true);
        $("#deleteButton").attr("disabled", true);
    },

    enableForm: function(msg) {
        $("#editImageLegend").text("Image Information");
        $("#insertEditButton").removeAttr("disabled");
        $("#deleteButton").removeAttr("disabled");
    }

};

$(document).ready(editImages.ready);